layui.use(['jquery', 'layer', 'form', 'upload', 'laydate', 'element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var element = layui.element;
    //获取使用人id
    var createId = localStorage.getItem('createId');
    $('input[name=updateId]').val(createId);
    //上传时间
    laydate.render({
        elem: '#createDate',
        type: 'date',
        trigger: 'click'
    });

    //下拉框
    dropDownBox();

    function dropDownBox() {
        $.ajax({
            url: "/courseClass/getCourseClass",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                var classId = localStorage.getItem('classId');
                for (var i = 0; i < d.length - 1; i++) {
                    var id = d[i].categroyId;
                    var name = d[i].title;
                    if (id == classId) {
                        $('#classId').append("<option value='" + id + "' selected>" + name + "</option>")
                    } else {
                        $('#classId').append("<option value='" + id + "'>" + name + "</option>")
                    }
                }
                form.render('select');
            }
        });
    }

    //修改图片
    var uploadInst = upload.render({
        elem: '#test',
        url: '/upload/uploadFile',
        field: 'files',
        accept: 'images',
        auto: true,
        acceptMime: 'image/*',
        before:function(obj){
            //预读本地文件
            obj.preview(function (index,file,result) {
                console.log(result);
                $('#url').attr('src', result);
            })
        },
        done: function (res, index, upload) {
            if (res.code == 0) {
                var url = res.data.path;
                $('input[name=url]').val(url);
            }
        },
        progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    //上传视频
    upload.render({
        elem: '#video',
        url: '/upload/uploadFile',
        field: 'files',
        accept: 'video',
        size:10240,
        exts: 'avi|rmvb|rm|wmv|mpg|mpeg|mov|ram|swf|flv|mp4|ogv',
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#courseUrl').attr('src', result);
                $('#courseUrl').attr('alt', file.name);
                //计算时长
                var minutes;
                var i = setInterval(function () {
                    var time = document.querySelector("#courseUrl");
                    var readyState = time.readyState;
                    if (readyState > 0) {
                        if (time.duration < 60) {
                            minutes = 1;
                        }else {
                            minutes = Math.ceil(time.duration/60);
                        }
                        $('input[name=courseTime]').val(minutes);
                        clearInterval(i);
                    }
                }, 200);
            });
        },
        done: function (res) {
            layer.msg('上传成功');
            console.log(res);
            $('input[name=courseUrl]').val(res.data.path);
        }
    });
    //修改课程
    form.on('submit(sub)',function (data) {
        $.ajax({
            url: '/course/updateCourse',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                if (data.data){
                    layer.msg("修改成功");
                    //关闭页面
                    close();
                    $.ajax({
                        url: '/course/getCourse',
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            parent.tablereload(data.data);
                        }
                    });
                } else {
                    layer.msg("修改失败")
                }
            }
        });
    });
    //取消
    $('#cancel').on('click',function () {
        close();
    });
    function close(){
        var layer=parent.layer;
        layer.close(layer.index);
    }
    //校验价格
    form.verify({
        price: function (value, item) {
            if (/^-[0-9]*[1-9][0-9]*$/.test(value)) {
                return '价格不能为负数';
            }
        }
    });

})